package com.ypj.community.controller;

import com.ypj.community.domain.DiscussPost;
import com.ypj.community.domain.Page;
import com.ypj.community.service.ElasticsearchService;
import com.ypj.community.service.LikeService;
import com.ypj.community.service.UserService;
import com.ypj.community.util.CommunityConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController implements CommunityConstant {
    @Autowired
    private ElasticsearchService elasticsearchService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikeService likeService;

    //search?keyword=???
    @RequestMapping(value = "/search",method = RequestMethod.GET)
    public String search(Model model, String keyword, Page page) throws IOException {
        //搜索帖子
        org.springframework.data.domain.Page<DiscussPost> searchResult=
                elasticsearchService.searchDiscussPost(keyword,page.getCurrent()-1,page.getLimit());
        //聚合数据
        List<Map<String,Object>> discussPosts=new ArrayList<>();
        if (searchResult!=null){
            for (DiscussPost discussPost: searchResult){
                Map<String,Object> map=new HashMap<>();
                //帖子
                map.put("post",discussPost);
                //作者
                map.put("user",userService.findUserById(discussPost.getUserId()));
                //点赞数量
                map.put("likeCount",likeService.findEntityLikeCount(ENTITY_TYPE_POST,discussPost.getId()));

                discussPosts.add(map);
            }
        }
        model.addAttribute("discussPosts",discussPosts);
        model.addAttribute("keyword",keyword);
        //分页信息
        page.setPath("/search?keyword="+keyword);
        page.setRows(searchResult==null?0:(int) searchResult.getTotalElements());
        return "/site/search";
    }
}
